//
//  YIem_SYToolBarView.m
//  B段_开眼
//
//  Created by YIem on 16/5/4.
//  Copyright © 2016年 Howie. All rights reserved.
//

#import "YIem_SYToolBarView.h"
#import "YIem_UIView+SYCategory.h"
#import "YIem_SY_Three_ViewController.h"
@implementation YIem_SYToolBarView
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        [self setupBtnWithImageName:@"tabbar_blog"];
        [self setupBtnWithImageName:@"tabbar_mood"];
        [self setupBtnWithImageName:@"tabbar_photo"];
    }
    return self;
}
- (void)setupBtnWithImageName:(NSString *)imageName {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"tabbar_separate_selected_bg"] forState:UIControlStateHighlighted];
    
//    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    NSInteger count = self.subviews.count;
    for (int i = 0; i < count; i++) {
        UIButton *btn = self.subviews[i];
        btn.tag = 10000 + i;
        [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.width = 70;
        btn.height = btn.width;
        btn.x = 0;
        btn.y = i * btn.height;
    }
}

- (void)buttonAction:(UIButton *)sender
{
//    NSLog(@"%d", sender.tag - 10000);
    if (sender.tag - 10000 == 1) {
        
        YIem_SY_Three_ViewController *threeVC = [[YIem_SY_Three_ViewController alloc] init];
         [threeVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        UINavigationController *NC = [[UINavigationController alloc] initWithRootViewController:threeVC];
        [[self viewController] presentViewController:NC animated:YES completion:nil];
    }
}
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
